package Day2025_02_27;

public class LeetCode912 {
    public int[] sortArray1(int[] nums) {
        // 冒泡排序
        int n = nums.length;
        for (int i = 0; i < n - 1; i++) {
            boolean tmp = true;
            for (int j = 0; j < n - i - 1; j++) {
                if (nums[j] > nums[j + 1]) swap(nums, j, j + 1);
                tmp = false;
            }
            if (tmp) break;
        }
        return nums;
    }
    private void swap (int[] nums, int i, int j) {
        int tmp = nums[i];
        nums[i] = nums[j];
        nums[j]= tmp;
    }

    public int[] sortArray2(int[] nums) {
        // 直接插入排序
        int n = nums.length;
        for (int i = 1; i < n; i++) {
            int j = i - 1;
            int tmp = nums[i];
            for ( ; j >= 0; j--) {
                if (nums[j] > tmp) nums[j + 1] = nums[j];
                else break;
            }
            nums[j + 1] = tmp;
        }
        return nums;
    }

    public int[] sortArray3(int[] nums) {
        // 希尔排序
        int gap = nums.length;
        while (gap > 1) {
            gap /= 2;
            shell(nums, gap);
        }
        return nums;
    }

    private void shell (int[] nums, int gap) {
        int n = nums.length;
        for (int i = gap; i < n; i++) {
            int j = i - gap;
            int tmp = nums[i];
            for ( ; j >= 0; j -= gap) {
                if (nums[j] > tmp) nums[j + gap] = nums[j];
                else break;
            }
            nums[j + gap] = tmp;
        }
    }

    public int[] sortArray4(int[] nums) {
        // 堆排序
        createHeap(nums);
        int end = nums.length - 1;
        while (end > 0) {
            swap(nums, 0, end);
            siftDown(nums, 0, end--);
        }
        return nums;
    }
    private void createHeap (int[] nums) {
        int n = nums.length - 1;
        int parent = (n - 1) / 2;
        for ( ; parent >= 0; parent--) siftDown(nums, parent, n + 1);
    }
    private void siftDown (int[] nums, int parent, int len) {
        int child = parent * 2 + 1;
        while (child < len) {
            if (child + 1 < len && nums[child] < nums[child + 1]) child++;
            if (nums[child] > nums[parent]) {
                swap(nums, child, parent);
                parent = child;
                child = parent * 2 + 1;
            } else break;
        }
    }
//    private void swap (int[] nums, int i, int j) {
//        int tmp = nums[i];
//        nums[i] = nums[j];
//        nums[j] = tmp;
//    }
public int[] sortArray5(int[] nums) {
    // 快速排序
    int n = nums.length - 1;
    quick(nums, 0, n);
    return nums;
}
    private void quick (int[] nums, int left, int right) {
        if (left >= right) return;
        int par = partition(nums, left, right);
        quick(nums, left, par - 1);
        quick(nums, par + 1, right);
    }
    private int partition (int[] nums, int start, int end) {
        int pivot = nums[start];
        while (start < end) {
            while (start < end && nums[end] >= pivot) end--;
            nums[start] = nums[end];
            while (start < end && nums[start] <= pivot) start++;
            nums[end] = nums[start];
        }
        nums[start] = pivot;
        return start;
    }

    public static void main(String[] args) {
        int[] nums = {110, 100, 0};
        LeetCode912 leetCode912 = new LeetCode912();
        System.out.println(leetCode912.sortArray5(nums));
    }

    public int[] sortArray(int[] nums) {
        // 归并排序
        int n = nums.length;
        mergeSort(nums, 0, n - 1);
        return nums;
    }
    private void mergeSort (int[] nums, int left, int right) {
        if (left == right) return;
        int mid = (right + left) / 2;
        mergeSort(nums, left, mid);
        mergeSort(nums, mid + 1, right);
        merge(nums, left, right, mid);
    }
    private void merge (int[] nums, int left, int right, int mid) {
        int s1 = left, e1 = mid;
        int s2 = mid + 1, e2 = right;
        int[] tmpArr = new int[right - left + 1];
        int i = 0;
        while (s1 <= e1 && s2 <= e2) {
            if (nums[s1] <= nums[s2]) tmpArr[i++] = nums[s1++];
            else tmpArr[i++] = nums[s2++];
        }
        while (s1 <= e1) tmpArr[i++] = nums[s1++];
        while (s2 <= e2) tmpArr[i++] = nums[s2++];
        for (i = 0; i < tmpArr.length; i++) nums[i + left] = tmpArr[i];
    }
}
